/*
	FileName : app_i2c1.c
	Description : 
		EEPROM FM24CL04B.
	
	Created on 2013.10.15 by ZZP.
	
	Revision History:
	
*/

#include "bsp.h"
#include "app.h"
#include "app_i2c1.h"
#include "JT_C_TIME.h"

void Test_I2C1(void);

void CalibrateTime(uint8_t tm_src, uint32_t tm1, uint32_t tm2);
uint16_t CheckTimeValid(uint8_t TmSrc, void *Buf);

RTC_TimeTypeDef LastTm;
RTC_DateTypeDef LastDate;


#if APP_I2C_1_ENABLE
OS_EVENT    *Sem_WriteReadI2C1;		///deadlock
#endif

//uint8_t i2c_read_time_buf[7];
uint8_t BSP_RTC_GetTime(uint8_t cur_time);

void  AppTaskI2C1 (void *p_arg)
{
	static uint16_t calif = 0;
	(void)p_arg;
	
	while (DEF_TRUE){

		if (AppInfo.RdyFlag == 1){
			
			if(!BSP_RTC_GetTime(1))
			{
			}
			if (AppInfo.TaxValidtype && JTC_TAX_checiValid()){
		
				if (!calif){
					
					calif = 1;
					AppInfo.TimeBeforeCalibrate = AppInfo.NowTime;
					BSP_RTC_SetTime(JTC_TAX_getTime());
					AppInfo.CalibrateTimeF = 1;
				}
			}
			//------------------------------------
		}
		
		OSTimeDly(200);
	}
}


//-----------------------------------------------------------------------

#define DS3232M_SLAVE_ADDR  0xD0
enum DS3232M_REG_ADDR
{
    DS3232M_REG_ADDR_SEC   = 0x00u,
    DS3232M_REG_ADDR_MIN   = 0x01u,
    DS3232M_REG_ADDR_HOUR  = 0x02u,
    DS3232M_REG_ADDR_DATE  = 0x04u,
    DS3232M_REG_ADDR_MON   = 0x05u,
    DS3232M_REG_ADDR_YEAR  = 0x06u,
    DS3232M_REG_ADDR_CTRL  = 0x0Eu,
    DS3232M_REG_ADDR_STAT  = 0x0Fu,
    DS3232M_REG_ADDR_AGING = 0x10u,
    DS3232M_REG_ADDR_TEMPH = 0x11u,
    DS3232M_REG_ADDR_TEMPL = 0x12u,
    DS3232M_REG_ADDR_TEST  = 0x13u,
    DS3232M_REG_ADDR_SRAM  = 0x14u
};

static uint8_t BCD2HEX(uint8_t bcd)
{
    uint8_t bcd10 = bcd >> 4;
    uint8_t bcd1  = bcd & 0xF;

    if (bcd10 > 9 || bcd1 > 9)
    {
        return 0xFF;
    }
    return (bcd10 * 10 + bcd1);
}

static uint8_t HEX2BCD(uint8_t hex)
{
    uint8_t tens = 0;
    
    if (hex > 99)
    {
        return 0xFF;
    }
    while (hex > 9)
    {
        hex -= 10;
        tens++;
    }
    return ((tens << 4) | hex);
}

uint8_t BSP_RTC_GetTime(uint8_t cur_time)
{
#if OS_CRITICAL_METHOD == 3                     
	OS_CPU_SR  cpu_sr = 0;
#endif
	
    uint8_t i,err;
	uint16_t cnt;
	uint8_t temp[7];
	uint32_t newtime;
	
    if (cur_time)
    {
		cnt = 7;
		OSSemPend(Sem_WriteReadI2C1,0,&err);
		err = BSP_ReadI2C1((uint8_t*)temp, DS3232M_REG_ADDR_SEC , &cnt);
		OSSemPost(Sem_WriteReadI2C1);
		
		if(err)
			return 1;
    }
    else
    {
		cnt = 7;
		OSSemPend(Sem_WriteReadI2C1,0,&err);
		err = BSP_ReadI2C1((uint8_t*)temp, DS3232M_REG_ADDR_SRAM , &cnt);
		OSSemPost(Sem_WriteReadI2C1);
		if(err)
			return 1;
    }
	
    for (i = 0; i < 7; i++)
    {
        temp[i] = BCD2HEX(temp[i]);
        if (temp[i] == 0xFF)
        {
            return 1;
        }
    }
	newtime = JTC_TIME_makeUpTime(temp[6],
									temp[5],
									temp[4],
									temp[2],
									temp[1],
									temp[0]);
	if(newtime != AppInfo.NowTime)
	{
		OS_ENTER_CRITICAL();
			AppInfo.OneSecAlarmFlag = 1;
			AppInfo.NowTime = newtime;
		OS_EXIT_CRITICAL();
	}
    return 0;
}

uint8_t BSP_RTC_SetTime(uint32_t jtc_t)
{
    uint8_t i,err;
    uint8_t temp[7];
	
	if(JTC_TIME_timeCheckErr(jtc_t))
		return 1;
	
	temp[0] = JTC_TIME_getSeco(jtc_t);
	temp[1] = JTC_TIME_getMinu(jtc_t);
	temp[2] = JTC_TIME_getHour(jtc_t);
	temp[3] = 1;
	temp[4] = JTC_TIME_getDate(jtc_t);
	temp[5] = JTC_TIME_getMont(jtc_t);
	temp[6] = JTC_TIME_getYear(jtc_t);
	
    temp[0] = HEX2BCD(temp[0]);
    temp[1] = HEX2BCD(temp[1]);
    temp[2] = HEX2BCD(temp[2]);
    temp[3] = 1;
    temp[4] = HEX2BCD(temp[4]);
    temp[5] = HEX2BCD(temp[5]);
    temp[6] = HEX2BCD(temp[6]);
    for (i = 0; i < 7; i++)
    {
        if (temp[i] == 0xFF)
        {
            return 1;
        }
    }
	
	OSSemPend(Sem_WriteReadI2C1,0,&err);
	
	BSP_WriteI2C1(temp, DS3232M_REG_ADDR_SEC, 7);
	BSP_WriteI2C1(temp, DS3232M_REG_ADDR_SRAM, 7);
	
	OSSemPost(Sem_WriteReadI2C1);
	
	
    return 0;
}

//EOF//---------------------------------------------------------------------
